package com.jxwifi.cloud.quickclean.assemble;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.utils.AutoPollRecyclerView;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.view.SuperSwipeRefreshLayout;
import com.youth.banner.Banner;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 拼团首页
 */
public class AssembleMainActivity extends ClientBasicActivity implements AssembleMainView {

    @Bind(R.id.assemble_main_banner)
    Banner mAssembleMainBanner;
    @Bind(R.id.rv_assemble_type)
    RecyclerView mRvAssembleType;
    @Bind(R.id.rv_assemble_main_list)
    AutoPollRecyclerView mRvAssembleMainList;
    @Bind(R.id.img_list_null)
    ImageView mImgListNull;
    @Bind(R.id.srl_assemble_ssf)
    SuperSwipeRefreshLayout mSrlAssembleSsf;
    @Bind(R.id.lin_assemble_main_banner)
    LinearLayout lin_assemble_main_banner;

    private AssembleMainPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assemble_main);
        ButterKnife.bind(this);
        openEventBus();
        initHead();
        mPresenter = new AssembleMainPresenter(mBaseContext, this);
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("拼团首页");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });

        DisplayMetrics dm =  getResources().getDisplayMetrics();
        ViewGroup.LayoutParams params=lin_assemble_main_banner.getLayoutParams();
        //params.height = 542;//参考小程序
        //params.height = (int)(206.4761904762 * dm.density);//参考小程序
        params.height = (int)(dm.widthPixels*dm.densityDpi/dm.density/317.0642075325);//参考小程序
        lin_assemble_main_banner.setLayoutParams(params);
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public SuperSwipeRefreshLayout getAssembleSsf() {
        return mSrlAssembleSsf;
    }

    @Override
    public ImageView getImgListNull() {
        return mImgListNull;
    }

    @Override
    public Banner getBanner() {
        return mAssembleMainBanner;
    }

    @Override
    public RecyclerView getAssembleType() {
        return mRvAssembleType;
    }

    @Override
    public AutoPollRecyclerView getAssembleMainList() {
        return mRvAssembleMainList;
    }

    @Override
    public void oninfo() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.getinfo();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onPresenterDestory();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getinfo();
    }
}
